<%
  text_areas = attributes.reject{ |a| a.field_type.to_s != 'text_area'}
  generated_text_areas = false
-%>
<%%= form_for [refinery, :<%= namespacing.underscore %>_admin, @<%= singular_name %>] do |f| -%>
  <%%= render '/refinery/admin/error_messages',
              :object => @<%= singular_name %>,
              :include_object_name => true %>

<% if localized? -%>
  <%%= render '/refinery/admin/locale_picker',
              :current_locale => Globalize.locale %>
<% end -%>
<% attributes.each_with_index do |attribute, index| -%>
<% if attribute.refinery_type == :image -%>
  <div class='field'>
    <%%= f.label :<%= attribute.name %> -%>
    <%%= render '/refinery/admin/image_picker',
               :f => f,
               :field => :<%= attribute.column_name %>,
               :image => @<%= singular_name %>.<%= attribute.name %>,
               :toggle_image_display => false -%>
  </div>

<% elsif attribute.refinery_type == :resource -%>
  <div class='field'>
    <%%= f.label :<%= attribute.name %> -%>
    <%%= render '/refinery/admin/resource_picker',
               :f => f,
               :field => :<%= attribute.column_name %>,
               :resource => @<%= singular_name %>.<%= attribute.name %> -%>
  </div>

<% elsif attribute.field_type.to_s == "text_area" and !generated_text_areas -%>
  <div class='field'>
    <%%= render '/refinery/admin/wysiwyg',
                :f => f,
                :fields => <%= text_areas.map{ |t| t.name.to_sym}.inspect -%>,
                :object => "<%= namespacing.underscore -%>/<%= singular_name -%>" -%>
  </div>

<% generated_text_areas = true -%>
<% elsif attribute.field_type.to_s != 'text_area' -%>
  <div class='field'>
    <%%= f.label :<%= attribute.name %> -%>
    <%%= f.<%= attribute.field_type -%> :<%= attribute.name -%><%= ", :class => 'larger widest'" if (index == 0 && attribute.field_type == :text_field) -%><%= ", :checked => @#{singular_name}[:#{attribute.name}]" if attribute.field_type == :check_box %> -%>
  </div>

<% end -%>
<% end -%>
  <%%= render '/refinery/admin/form_actions', :f => f,
             :continue_editing => false,
             :delete_title => t('delete', :scope => 'refinery.<%= plural_name %>.admin.<%= plural_name %>.<%= singular_name %>'),
             :delete_confirmation => t('message', :scope => 'refinery.admin.delete'<% if (title = attributes.detect { |a| a.type.to_s == "string" }).present? %>, :title => @<%= singular_name %>.<%= title.name %><% end %>) -%>
<%% end -%>
<% if text_areas.any? -%>

<%% content_for :javascripts do -%>
  <script>
    $(document).ready(function(){
      page_options.init(false, '', '');
    });
  </script>
<%% end -%>
<% end -%>
